import socket

if __name__ == '__main__':
    ip_local = ("127.0.0.1", 8000)
    sk = socket.socket()
    sk.bind(ip_local)
    max_clients = 5  # 该网站仅能监听5个client 一旦超过就会报错
    sk.listen(max_clients)
    ip_pool = []
    ip_pool_dic = {}
    connects = 0
    while True:
        print('server is running')
        connect, address = sk.accept()
        ipadress = connect.getpeername()[0]
        if ipadress not in ip_pool:
            ip_pool.append(ipadress)
            dic = {ipadress: 1}
            ip_pool_dic.update(dic)
            connects += 1
        else:
            if connects >= max_clients:
                connect.send(b"HTTP/1.1 404 ERROR\r\n")
            else:
                ip_pool_dic[ipadress] += 1
                connects += 1
        data = connect.recv(1024)
        connect.send(b"HTTP/1.1 200 OK\r\nContent-Type:text/html;charset=utf-8\r\nConnection:Keep-Alive\r\n\r\n")
        connect.send("<html><head><title>测试使用</title><head><body><h1>仅供测试</h1></body></html>".encode('utf-8'))

        connect.close()
